package com.controller.developerPortal;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Document;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import dao.DocumentDAO;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/download/")
public class DownloadFileController {
	 private static final int BUFFER_SIZE = 4096;	
	    @Autowired
	    ServletContext context; 
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("springConfigSQL.xml");
		DocumentDAO documentDAO = ctx.getBean("documentDAO",DocumentDAO.class);
	 @RequestMapping(value = "/", method = RequestMethod.GET)
	    public String index() {
	        return "downloadFile";
	   		    }
	 
	 @RequestMapping(value = "/", method = RequestMethod.POST)
	    public String download(int docId,String password,HttpServletRequest request,
	            HttpServletResponse response,Model model) throws IOException {
		 	Document document = new Document();
		 	document = documentDAO.getById(docId);
		 	
	        String fullPath = document.getFilePath(); 
	        int realPassword = document.getNumberOfDownload();
	        System.out.println(realPassword + "---" +password);
	        int intPassword = Integer.parseInt(password);
		    if ((realPassword>0)&&(realPassword==intPassword)) {
				File downloadFile = new File(fullPath);
				FileInputStream inputStream = new FileInputStream(downloadFile);
				// get MIME type of the file
				String mimeType = context.getMimeType(fullPath);
				if (mimeType == null) {
					// set to binary type if MIME mapping not found
					mimeType = "application/octet-stream";
				}

				System.out.println("MIME type: " + mimeType);
				// set content attributes for the response
		        response.setHeader("Content-Disposition", "inline; filename=\"1.pdf\"");
		        response.setDateHeader("Expires", -1);
		        response.setContentType("application/pdf");
		        response.setContentLength((int) downloadFile.length());
				String headerValue = String.format(
						"attachment; filename=\"%s\"", downloadFile.getName());
				// get output stream of the response
				OutputStream outStream = response.getOutputStream();
				byte[] buffer = new byte[BUFFER_SIZE];
				int bytesRead = -1;
				// write bytes read from the input stream into the output stream
				while ((bytesRead = inputStream.read(buffer)) != -1) {
					outStream.write(buffer, 0, bytesRead);
				}
				inputStream.close();
				outStream.close();
				document.setNumberOfDownload(0);
				documentDAO.update(document);

			}
			return "downloadFileResult";
	   		    }
	   	
}
